const tencentcloud = require('tencentcloud-sdk-nodejs');
const { clientConfig, mergeAgent, logger } = require('../utils');

const EssbasicClient = tencentcloud.essbasic.v20210526.Client;

// 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
// 实例化要请求产品的client对象,clientProfile是可选的
const client = new EssbasicClient(clientConfig);

module.exports = (app) => {
  // 跳转外部链接
  app.post(`${process.env.VUE_APP_BASE_API}/external/detail`, async (req, res) => {
    const { body, userInfo } = req;
    const { Module, ModuleId, MenuStatus = 'ENABLE' } = body;
    const targetUser = userInfo;
    let params = {};
    if (targetUser) {
      params = {
        Agent: mergeAgent({
          ProxyOrganizationOpenId: targetUser.organizationOpenId,
          OpenId: targetUser.openId,
        }),
        ProxyOrganizationName: targetUser.organizationName,
        ProxyOperatorName: targetUser.userName,
        Module,
        ModuleId,
        UniformSocialCreditCode: targetUser.uniformSocialCreditCode,
        Endpoint: 'PC',
        MenuStatus,
      };
    }

    client.CreateConsoleLoginUrl(params).then(
      (data) => {
        if (data.ConsoleUrl) {
          return res.json({
            code: 20000,
            data,
          });
        }
        return res.json(data);
      },
      (err) => {
        logger.error({
          ...params,
          Action: 'CreateConsoleLoginUrl',
          message: err,
        });
        res.json({
          code: 500,
          err,
        });
      },
    );
  });

  // 印章列表
  app.post(`${process.env.VUE_APP_BASE_API}/seal/list`, async (req, res) => {
    const { userInfo, token, body } = req;
    const targetUser = userInfo;

    if (targetUser) {
      const params = {
        Agent: mergeAgent({
          ProxyOrganizationOpenId: targetUser.organizationOpenId,
          OpenId: targetUser.openId,
        }),
        ...body,
      };
      client.ChannelDescribeOrganizationSeals(params).then(
        (data) => {
          res.json({
            code: 20000,
            data,
          });
        },
        (err) => {
          logger.error({
            ...params,
            Action: 'ChannelDescribeOrganizationSeals',
            message: err,
          });
          res.json(err);
        },
      );
    } else {
      logger.error({
        Action: 'seal/list',
        token,
        message: '用户信息不存在',
      });
      res.json({
        code: 500,
        message: '用户信息不存在',
      });
    }
  });


  // 操作渠道模板
  app.post(`${process.env.VUE_APP_BASE_API}/template/OperateChannelTemplate`, async (req, res) => {
    const { userInfo, body } = req;
    const targetUser = userInfo;
    console.log('targetUser====', targetUser);
    if (targetUser) {
      const params = {
        Agent: mergeAgent({
          ProxyOrganizationOpenId: targetUser.organizationOpenId,
          OpenId: targetUser.openId,
          ProxyAppId: targetUser?.proxyAppId,
          // AppId: '',
        }),
        Version: '2021-05-26',
        ...body,
      };
      client
        .OperateChannelTemplate(params)
        .then(
          (data) => {
            res.json({
              code: 20000,
              data,
            });
          },
          (err) => {
            console.log('err====', err);
            logger.error({
              ...params,
              Action: 'OperateChannelTemplate',
              message: err.message,
              stack: err.stack,
            });
            res.json(err);
          },
        )
        .catch((error) => {
          console.log('error====', error);
        });
    } else {
      res.json({
        code: 500,
      });
    }
  });
};
